<template>
  <div class="hello">
    <el-table 
      :data="tableData" 
      style="width: 100%" 
      max-height="500">
      <el-table-column
        fixed
        prop="id"
        label="编号"
        width="150">
      </el-table-column>
      <el-table-column
        prop="title"
        label="书名"
        width="250">
      </el-table-column>
      <el-table-column
        prop="author"
        label="作者"
        width="250">
      </el-table-column>
      <el-table-column
        prop="copies"
        label="余量"
        width="250">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="deleteRow(scope.$index, tableData)"
            type="text"
            size="small">
            移除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="add-book">
      <el-input v-model="newBook.id" placeholder="编号" width="120"></el-input>
      <el-input v-model="newBook.title" placeholder="书名" width="120"></el-input>
      <el-input v-model="newBook.author" placeholder="作者" width="120"></el-input>
      <el-input v-model="newBook.copies" placeholder="余量" width="120"></el-input>
      <el-button @click="addBook">添加</el-button>
    </div>
  </div>
</template>

<script>
export default {
  title: 'HelloWorld',
  props: {
    msg: String
  },
  data() {
    return {
      tableData: [
        {"id": 1, "title": "Python编程", "author": "John Doe", "copies": 4},
        {"id": 2, "title": "历史的秘密", "author": "Jane Smith", "copies": 3},
        {"id": 3, "title": "宇宙探索", "author": "Emily Brown", "copies": 2},
        {"id": 4, "title": "健康生活", "author": "Mike Johnson", "copies": 5},
        {"id": 5, "title": "数学之美", "author": "Sarah Lee", "copies": 4},
        {"id": 6, "title": "艺术欣赏", "author": "David White", "copies": 3},
        {"id": 7, "title": "编程入门", "author": "Alice Green", "copies": 2},
        {"id": 8, "title": "科幻世界", "author": "Frank Black", "copies": 5},
        {"id": 9, "title": "自然奇观", "author": "Grace Blue", "copies": 4},
        {"id": 10, "title": "音乐之声", "author": "Tom Red", "copies": 3}
      ],
      newBook: {
        id: '',
        title: '',
        author: '',
        copies: ''
      }
    };
  },
  methods: {
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    addBook() {
      if (this.newBook.id && this.newBook.title && this.newBook.author && this.newBook.copies) {
        this.tableData.push({
          id: this.newBook.id,
          title: this.newBook.title,
          author: this.newBook.author,
          copies: this.newBook.copies
        });
        this.newBook.id = '';
        this.newBook.title = '';
        this.newBook.author = '';
        this.newBook.copies = '';
      } else {
        this.$message.error('请填写所有字段');
      }
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.add-book {
  margin-top: 20px;
}
</style>
